%judge the gridding is correct or not
%hf: the boundary grid line number in horizontal
%vf: the boundary grid line number in vertical
%hspotcoff: coordinate of grid line in horizontal
%vspotcoff: coordinate of grid line in vertical
%correct:gridding is correct when correct=1 
function correct=iscorrect(hf,vf,hspotcoff,vspotcoff)
[s,hf1]=size(hspotcoff);
[s,vf1]=size(vspotcoff);
disp(['number of horizontal grid lines��',num2str(hf1),';','number of vertial grid lines��',num2str(vf1)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%distance between two grid lines
for m=1:hf1-1
 hd(m)=hspotcoff(m+1)-hspotcoff(m);
end
for n=1:vf1-1
 vd(n)=vspotcoff(n+1)-vspotcoff(n);
end
%figure,plot(1:hf1-1,hd,'mean distance in horizontal');
%figure,plot(1:vf1-1,vd,'mean distance in vertial');
%mean distance
hdmean=mean(hd);
vdmean=mean(vd);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%distance error
for m=1:hf1-1
 hderror(m)=abs(hd(m)-hdmean);
end
for n=1:vf1-1
 vderror(n)=abs(vd(n)-vdmean);
end
%figure,plot(1:hf1-1,hderror,'distance error profile in horizontal');
%figure,plot(1:vf1-1,vderror,'distance error profile in vertial');
%mean distance error
hdermean=mean(hderror);
vdermean=mean(vderror);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%mean square error
herror=0;
for m=1:hf1-1
    herror=herror+(hd(m)-hdmean).^2;
end
herror=sqrt(herror/(hf1-1))
verror=0;
for m=1:vf1-1
    verror=verror+(vd(m)-vdmean).^2;
end
verror=sqrt(verror/(vf1-1))
disp(['mean distance in vertical��',num2str(vdmean),';','mean distance error in vertical��',num2str(vdermean)]);
disp(['mean distance in horizontal��',num2str(hdmean),';','mean distance error in horizontal��',num2str(hdermean)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%estimate the number of wrong grid lines
hernum=0;
for m=2:hf1-2
    if hderror(m)>herror+hdmean/10
        hernum=hernum+1;
    end
end
vernum=0;
for n=2:vf1-2
    if vderror(n)>verror+vdmean/10
        vernum=vernum+1;
    end
end
disp(['the wrong grid line numbers in horizontal:',num2str(hernum),';','the wrong grid line numbers in vertical:',num2str(vernum)]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%judge the boundary grid line number is odd or not
if mod(hf,2)==0 && mod(vf,2)==0
    c(1)=1;
else
    c(1)=0;
end
if hernum==0 && vernum==0
    c(2)=1;
else
    c(2)=0;
end
correct=c(1)&c(2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear hf;
clear vf;
clear hspotcoff;
clear vspotcoff;
clear c;
